package com.yzkj.cpjxc.business.controller;

import com.yzkj.cpjxc.business.domain.*;
import com.yzkj.cpjxc.business.framework.common.BusinessMap;
import com.yzkj.cpjxc.business.framework.common.CSRFTokenManager;
import com.yzkj.cpjxc.business.framework.common.Constants;
import com.yzkj.cpjxc.business.framework.utils.CollectionHelp;
import com.yzkj.cpjxc.business.framework.utils.ImageAndCode;
import com.yzkj.cpjxc.business.service.JieZhangService;
import com.yzkj.cpjxc.business.service.SysDeptService;
import com.yzkj.cpjxc.business.service.SysUserService;
import com.yzkj.cpjxc.business.service.ZhangTaoService;
import com.yzkj.cpjxc.business.system.interceptor.DoLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 用户登录控制器
 */
@Controller
@RequestMapping("/login")
public class LoginController extends BaseController {
	
	@Autowired
	private SysUserService sysUserService;
	
	@Autowired
	private SysDeptService sysDeptService;
	
	@Autowired
	private ZhangTaoService zhangTaoService;
	
	@Autowired
	private JieZhangService jieZhangService;

	@RequestMapping("/toLogin") @DoLog(cnContent = "去到主页面", value = false)
	public ModelAndView toLogin(HttpServletRequest request) throws Exception {
		return new ModelAndView("login").addObject("csrfToken", CSRFTokenManager.getTokenForSession(request.getSession()));
	}
	
	@RequestMapping("/login") @DoLog(false)
	public String login(HttpServletRequest request, HttpServletResponse response, SysUser sysUser) throws Exception {
		try {
			
			//获取验证码
			String newCode = request.getParameter("code");
			//验证码校验
			String oldCode = (String)request.getSession().getAttribute("code");
			if(newCode!=null && !newCode.equalsIgnoreCase(oldCode)){
				request.getSession().removeAttribute("code");
				//return this.ajax(response, "验证码不正确！");
			}
			
			// 1、 根据获取的参数调用逻辑层api
			BusinessMap bm = sysUserService.login(sysUser);
			// 2、 根据返回的结果进行页面跳转
			if (bm.getIsSucc()) {
				// 3、当前用户放入session
				request.getSession().setAttribute(Constants.CURRENT_USER, bm.getFromDataMap(Constants.CURRENT_USER));
				// 4、当前部门区划放入session
				List<SysDeptShowVo> udList = sysDeptService.getDeptByUserId(((SysUser)bm.getFromDataMap(Constants.CURRENT_USER)).getUserId());
				if(CollectionHelp.isNotBank(udList)){
					if(udList.size()==1){
					}else{
						
					}
				}
				
				return this.ajax(response, Constants.SUCCESS);
			} else {
				return this.ajax(response, bm.getMess(Constants.WARNING_MSG));
			}
		} catch (Exception e) {
			this.logException(e);
			return this.ajax(response, "系统出现异常: [" + e.getMessage() + "] 请与管理员联系！");
		}
	}
	
	@RequestMapping("/getCode") @DoLog(cnContent = "获取验证码", value = false)
	public String getCode(HttpServletRequest request, HttpServletResponse response) throws Exception {
		try {
			
			//设置响应的类型格式为图片格式
	        response.setContentType("image/jpeg");
	        //禁止图像缓存。
	        response.setHeader("Pragma", "no-cache");
	        response.setHeader("Cache-Control", "no-cache");
	        response.setDateHeader("Expires", 0);
	        
	        ImageAndCode vCode = new ImageAndCode(100,35,4,2);
	        request.getSession().setAttribute("code", vCode.getCode());
	        vCode.write(response.getOutputStream());
	        
	        return null;
		} catch (Exception e) {
			this.logException(e);
			return this.ajax(response, "系统出现异常: [" + e.getMessage() + "] 请与管理员联系！");
		}
	}

	@RequestMapping("/toMain") @DoLog(cnContent = "去到main主页面", value = false)
	public ModelAndView toMain(HttpServletRequest request) throws Exception {
		
		String uuid = request.getParameter("uuid");
		ZhangTao zhangTao = zhangTaoService.getById(uuid);
		//避免值被页面刷新操作冲掉
		if(zhangTao!=null){
			request.getSession().setAttribute(Constants.CURRENT_ZHANG_TAO, zhangTao);
			JieZhang jieZhang = jieZhangService.getByZhangTaoId(zhangTao.getUuid());
			request.getSession().setAttribute(Constants.CURRENT_JIE_ZHANG, jieZhang);
		}
		
		return new ModelAndView("main").addObject("csrfToken", CSRFTokenManager.getTokenForSession(request.getSession()));
	}
	
	@RequestMapping("/toztMain") @DoLog(cnContent = "去到main主页面", value = false)
	public ModelAndView toztMain(HttpServletRequest request) throws Exception {
		return new ModelAndView("ztMain").addObject("csrfToken", CSRFTokenManager.getTokenForSession(request.getSession()));
	}
	
	@RequestMapping("/toWelcome") @DoLog(cnContent = "欢迎页面(首页)", value = false) //@Permission(name = "login.view")
	public ModelAndView toWelcome(HttpServletRequest request) throws Exception {
		return new ModelAndView("welcome").addObject("csrfToken", CSRFTokenManager.getTokenForSession(request.getSession()));
	}
	
	@RequestMapping("/toztWelcome") @DoLog(cnContent = "欢迎页面(首页)", value = false)
	public ModelAndView toztWelcome(HttpServletRequest request) throws Exception {
		return new ModelAndView("ztWelcome").addObject("csrfToken", CSRFTokenManager.getTokenForSession(request.getSession()));
	}

	@RequestMapping("/toLeft") @DoLog(cnContent = "去到主页面", value = false)
	public ModelAndView toLeft(HttpServletRequest request) throws Exception {
		return new ModelAndView("layout/left").addObject("csrfToken", CSRFTokenManager.getTokenForSession(request.getSession()));
	}

	@RequestMapping("/toRight") @DoLog(cnContent = "去到左页面", value = false)
	public ModelAndView toRight(HttpServletRequest request) throws Exception {
		return new ModelAndView("layout/right").addObject("csrfToken", CSRFTokenManager.getTokenForSession(request.getSession()));
	}

	@RequestMapping("/toTop") @DoLog(cnContent = "去到顶页面", value = false)
	public ModelAndView toTop(HttpServletRequest request) throws Exception {
		return new ModelAndView("layout/top").addObject("csrfToken", CSRFTokenManager.getTokenForSession(request.getSession()));
	}

	@RequestMapping("/toBottom") @DoLog(cnContent = "去到底页面", value = false)
	public ModelAndView toBottom(HttpServletRequest request) throws Exception {
		return new ModelAndView("layout/bottom").addObject("csrfToken", CSRFTokenManager.getTokenForSession(request.getSession()));
	}

	@RequestMapping("/logout") @DoLog(cnContent = "注销session", value = false)
	public String logout(HttpServletRequest request, HttpServletResponse response) throws Exception {
		try {
			request.getSession().invalidate();
			return this.ajax(response, Constants.SUCCESS);
		} catch (Exception e) {
			this.logException(e);
			return this.ajax(response, "系统出现异常: [" + e.getMessage() + "] 请与管理员联系！");
		}
	}

	@RequestMapping("/toChangePass") @DoLog(cnContent = "去到修改密码页面", value = false)
	public ModelAndView toChangePass(HttpServletRequest request) throws Exception {
		return new ModelAndView("layout/changePass").addObject("csrfToken", CSRFTokenManager.getTokenForSession(request.getSession()));
	}
	
	@RequestMapping("/changePass") @DoLog(operationAction = "U", value = false)
	public String changePass(HttpServletRequest request, HttpServletResponse response, SysUserEditPassVo vo) throws Exception {
		try {
			SysUser currentUser = (SysUser)this.getLoginUser(request);
			vo.setUserId(currentUser.getUserId());
			vo.setCurrentPassword(currentUser.getPassword());
			BusinessMap bm = sysUserService.changePass(vo);
			if (bm.getIsSucc()) {
				return this.ajax(response, Constants.SUCCESS);
			} else {
				return this.ajax(response, bm.getMess(Constants.WARNING_MSG));
			}
		} catch (Exception e) {
			this.logException(e);
			return this.ajax(response, "系统出现异常: [" + e.getMessage() + "] 请与管理员联系！");
		}
	}

}
